package com.android.SchoolManagerSystem.net;
import org.apache.http.HttpVersion;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRoute;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class PubMethod {

	public final static String SCHEME_HTTP = "http";
	public final static String SCHEME_HTTPS = "https";
	public final static int HTTP_PORT = 80;
	public final static int HTTPS_PORT = 443;
	public final static String defaultContentEncoding = HTTP.UTF_8;

	/**
	 * Check whether current network is connected
	 * 
	 * @author jeson_li
	 * @date 2011-5-20
	 * @param context
	 * @return <code>true</code> or <code>false</code>
	 */
	public static boolean isNetworkConnected(Context context) {
		try {
			ConnectivityManager cm = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo network = cm.getActiveNetworkInfo();
			if (network != null)
				return network.isAvailable();
			return false;
		} catch (Exception e) {
			// TODO: handle exception
			
				e.printStackTrace();
			return false;
		}

	}
	/**
	 * Get DefaultHttpClient to send http request
	 * 
	 * @author jeson_li
	 * @date 2011-5-9
	 * @return DefaultHttpClient
	 */
	public static DefaultHttpClient getDefaultHttpClient() {
		// Set basic data
		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params, defaultContentEncoding);
		HttpProtocolParams.setUseExpectContinue(params, true);
		HttpProtocolParams.setUserAgent(params, "HTTPWebUtil/1.0.1");

		// Make pool
		ConnPerRoute connPerRoute = new ConnPerRouteBean(12);
		ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
		ConnManagerParams.setMaxTotalConnections(params, 20);

		// Set timeout
		HttpConnectionParams.setStaleCheckingEnabled(params, false);
		HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
		HttpConnectionParams.setSoTimeout(params, 20 * 1000);
		HttpConnectionParams.setSocketBufferSize(params, 8192);

		// Some client params
		HttpClientParams.setRedirecting(params, true);

		// Register http/s shemas!
		SchemeRegistry schReg = new SchemeRegistry();
		schReg.register(new Scheme(SCHEME_HTTP, PlainSocketFactory
				.getSocketFactory(), HTTP_PORT));
		schReg.register(new Scheme(SCHEME_HTTPS, SSLSocketFactory
				.getSocketFactory(), HTTPS_PORT));

		ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
				params, schReg);

		return new DefaultHttpClient(conMgr, params);
	}


}
